﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DTO;
using BUS;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using Telerik.Web.UI;

public partial class admin_Menus : System.Web.UI.Page
{
    PositionBUS positionBUS = new PositionBUS();
    PanelBUS panelBUS = new PanelBUS();
    List<PositionDTO> positionDTO;
    List<PanelDTO> panelMainDTO;
    List<PanelDTO> panelSubDTO;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["adminName"] == null)
            Response.Redirect("login.aspx?turn=menus.aspx");

        if (!IsPostBack)
        {
            LoadMainPanel();
            LoadCbbPosition();
        }
    }

    protected void LoadCbbPosition()
    {
        //load Position
        positionDTO = positionBUS.GetAllPositions();
        cbbPositions.Items.Clear();
        foreach(var pos in positionDTO)
        {
            cbbPositions.Items.Add(new Telerik.Web.UI.RadComboBoxItem(pos.PositionID + " - " + pos.PositionName, pos.PositionID.ToString()));
        }
    }

    protected void LoadMainPanel()
    {
        //load Main Panel
        panelMainDTO = panelBUS.GetAllActiveMainPanels();
        cbbParent.Items.Clear();
        foreach (var pan in panelMainDTO)
        {
            cbbParent.Items.Add(new Telerik.Web.UI.RadComboBoxItem(pan.PanelID +" - "+ pan.Name, pan.PanelID.ToString()));
        }
        cbbParent.AutoPostBack = true;
       grvMainPanel.DataSource = panelMainDTO;
       grvMainPanel.Rebind();
    }

    protected void LoadSubPanel(int mainPanelID)
    {
        //load Sub Panel
        panelSubDTO = panelBUS.GetActiveSubPanels(mainPanelID);
        grvSubPanel.DataSource = panelSubDTO;
        grvSubPanel.Rebind();
    }

    protected void grvMainPanel_OnSelectedIndexChanged(object o, EventArgs e)
    {
        try
        {
            int mainPanelID = Int32.Parse(grvMainPanel.SelectedValue.ToString());
            cbbParent.SelectedValue = mainPanelID.ToString();
            cbbParent_OnSelectedIndexChanged(new object(), new EventArgs());
        }
        catch (Exception)
        { return; }
    }

    protected void cbbParent_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        LoadSubPanel(Int32.Parse(cbbParent.SelectedValue));
    }

    public void grvMainPanel_OnDeleteCommand(object sender, GridCommandEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        int id = Int32.Parse(dataItem.GetDataKeyValue("panelID").ToString());
        panelBUS.DeletePanel(id);
        LoadMainPanel();

    }

    public void grvSubPanel_OnDeleteCommand(object sender, GridCommandEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        int id = Int32.Parse(dataItem.GetDataKeyValue("panelID").ToString());
        panelBUS.DeletePanel(id);
        LoadSubPanel(Int32.Parse(cbbParent.SelectedValue));
    }

    public void btnAddMain_OnClick(object sender, EventArgs e)
    {
        if (txtMainMenu.Text == null || txtMainMenu.Text == "")
            return;
        PanelDTO panel = new PanelDTO();
        panel.Name = txtMainMenu.Text;
        panel.IsActived = true;
        panel.ParentsID = null;
        panel.PositionID = Int32.Parse(cbbPositions.SelectedValue);
        panel.SortNumber = grvMainPanel.Items.Count + 1;
        panel.Description = "";
        if (panelBUS.AddPanel(panel))
            LoadMainPanel();
    }

    public void btnAddSub_OnClick(object sender, EventArgs e)
    {
        if (txtSubMenu.Text == null || txtSubMenu.Text == "")
            return;
        PanelDTO panel = new PanelDTO();
        panel.Name = txtSubMenu.Text;
        panel.IsActived = true;
        panel.ParentsID = Int32.Parse(cbbParent.SelectedValue);
        panel.PositionID = null;
        panel.SortNumber = grvSubPanel.Items.Count + 1;
        panel.Description = "";
        if (panelBUS.AddPanel(panel))
            LoadSubPanel(Int32.Parse(cbbParent.SelectedValue));
    }

    protected void grvMainPanel_OnRowDrop(object sender, GridDragDropEventArgs e)
    {
        ArrayList arr = new ArrayList();
        for (int i = 0; i < grvMainPanel.Items.Count; i++)
        {
            arr.Add(Int32.Parse(grvMainPanel.Items[i].GetDataKeyValue("panelID").ToString()));
        }

        panelBUS.ReOrder(arr);
        
    }

    protected void grvSubPanel_OnRowDrop(object sender, GridDragDropEventArgs e)
    {
        ArrayList arr = new ArrayList();
        for (int i = 0; i < grvSubPanel.Items.Count; i++)
        {
            arr.Add(Int32.Parse(grvSubPanel.Items[i].GetDataKeyValue("panelID").ToString()));
        }

        panelBUS.ReOrder(arr);

    }
}
